//== SAGITTARIUS ============================================================================

//== BEGINNING OF CODE ===============================================================

#include "sagittarius/core/SagittariusCore.h"

//SAGITTARIUS headers:
#include "sagittarius/mem/dumballoc.h"

#include <malloc.h>
#include <string.h>

void dumballoc_init(dumballoc_list* d) {
    d->n = 0;
    d->c = 64;
    d->a = malloc(sizeof(void*)*sizeof(d->c));
}

void* dumballoc(dumballoc_list* d, size_t s) {
    //TODO: locking for concurrency
    //LONG-TERM TODO: make it, you know, not dumb
    void** t;
    void* p;
    if(d->n >= d->c) {
        t = malloc(sizeof(void*)*sizeof(d->c<<1));
        memcpy(t, d->a, sizeof(void*)*sizeof(d->c));
        d->c <<= 1;
        free(d->a);
        d->a = t;
    }
    p = malloc(s);
    d->a[d->n++] = p;
    return p;
}

void dumbfree(dumballoc_list* d) {
    size_t i;
    for(i=0;i<d->n;++i) {
        free(d->a[i]);
    }
    free(d->a);
}


